from csr_utils import log

from server import db_session

#todo 遗留问题 ⭐️ 导致的问题: session 管理
#
class BaseDao:

    def query(cls, entity,*args,**kwargs):
        """
        entity ： 实体类
        """
        return db_session.query(entity,*args,**kwargs)

    def add(cls, entity,**kwargs):
        try:
            db_session.add(entity)
            db_session.commit()
            entity_id = entity.id
            # db_session.close()
            return entity_id
        except Exception as e:
            log.error(f"提交数据发生了错误，错误信息为：{e}")
            return False
    def update(self,*args,**kwargs):
        try:
            db_session.commit()
            # db_session.close()
        except Exception as e:
            log.error(f"更新数据发生了错误，错误信息为：{e}")
            return False
    def delete(cls, entity):
        try:
            db_session.delete(entity)
            db_session.commit()
            # db_session.close()
            return True
        except Exception as e:
            log.error(f"删除数据发生了错误，错误信息为：{e}")
            return False

    def commit(cls,*args,**kwargs):
        try:
            db_session.commit()
            # db_session.close()
            return True
        except Exception as e:
            log.error(f"更新数据发生了错误，错误信息为：{e}")
            return False

    def close_session(cls,*args,**kwargs):
        db_session.close()
